﻿using UnityEngine;
using System.Collections;

//'salvaged' from other project of mine- camera designed to track one primary character as well as another if in certain range.  
// now tracks player and mouse 

public class TrackCamera : MonoBehaviour {

	Transform Trnsfrm;
    Camera Cam;
    float PosZ;
    public float MaxZoom = 16, MinZoom = 8, PlayerRadius = 6, BreakMod = 1.1f, PanSpeed = 0.1f, ZoomSpeed = 0.2f;
   // PlayerInput PIn;
    PlayerController Current, Other;

    public BoxCollider Bounds;

    Vector2 DesP; float DesZ;

    void Awake() {
        Trnsfrm = transform;
        Cam = camera;
        DesZ = Cam.orthographicSize;
    }

    void Start() {
        PosZ = Trnsfrm.position.z;
        DesP = (Vector2)Trnsfrm.position;

        Current = FindObjectOfType<PlayerController>(); 
        //  var pc = FindObjectsOfType<PlayerController>(); if(pc.Length <= 0) return;
        
        
      //  Current = Other = pc[0];
        //if( pc.Length >1 ) Other = pc[1];
    }


    bool TrackOther = false;
	void FixedUpdate () {

        float mnX = Bounds.bounds.min.x, mxX = Bounds.bounds.max.x;
        float mnY = Bounds.bounds.min.y, mxY = Bounds.bounds.max.y;

        var mPos = Input.mousePosition;
        mPos.x = Mathf.Clamp(mPos.x, 0, Screen.width);
        mPos.y = Mathf.Clamp(mPos.y, 0, Screen.height);

        Vector2 c = (Vector2)Current.Trnsfrm.position, o = (Vector2)Camera.main.ScreenToWorldPoint(mPos);

        Vector2 trv = new Vector2(PlayerRadius,PlayerRadius);

        Vector2 bl = c, tr = c;//, diffV = c-o;
       /* float diff = Mathf.Max(Mathf.Abs(diffV.x / Cam.aspect), Mathf.Abs(diffV.y));

        if(TrackOther) 
           TrackOther =  diff < (MaxZoom - PlayerRadius) * 2.0f * BreakMod;
        else 
           TrackOther =  diff < (MaxZoom - PlayerRadius) * 2.0f * BreakMod*0.75;
        
      //  if(TrackOther) {*/
            bl -= trv; tr += trv;
            trv *= 0.4f;

            bl.x = Mathf.Min(bl.x, o.x - trv.x);
            bl.y = Mathf.Min(bl.y, o.y - trv.y);
            tr.x = Mathf.Max(tr.x, o.x + trv.x);
            tr.y = Mathf.Max(tr.y, o.y + trv.y);
            // if(c.x > o.x) bl.x = o.x - trv.x; else tr.x = o.x - trv.x; ;
            //if(c.y > o.y) bl.y = o.y - trv.y; else tr.y = o.y + trv.x;
       
            bl.x = Mathf.Max(bl.x, mnX); bl.y = Mathf.Max(bl.y, mnY);
            tr.x = Mathf.Min(tr.x, mxX); tr.y = Mathf.Min(tr.y, mxY);
      //  }
       // bl.x = mnX; tr.x = mxX;

        //Debug.Log("bl.x, mnX  " + bl.x + "    " + tr.x + "    " + mnX + "    " + mxX);

      // */
        Vector2 size = (tr - bl)*0.5f;
        size.x /= Cam.aspect;

        DesZ = Mathf.Clamp( Mathf.Max( size.x, size.y), MinZoom, MaxZoom );

        Cam.orthographicSize = Mathf.Lerp(Cam.orthographicSize, DesZ, ZoomSpeed * Time.deltaTime);

        float hHght = Cam.orthographicSize;
        float hWid = hHght * Cam.aspect;

        mnX += hWid; mxX -= hWid;
        mnY += hHght; mxY -= hHght;
        if(mnX > mxX) mnX = mxX = (mnX + mxX) * 0.5f;
        if(mnY > mxY) mnY = mxY = (mnY + mxY) * 0.5f;

        
        var dp = (bl+tr)*0.5f;
        dp.x = Mathf.Clamp(dp.x, mnX, mxX);
        dp.y = Mathf.Clamp(dp.y, mnY, mxY);


     //   DesP = Vector2.Lerp(dp, DesP, 0.25f);
        DesP = dp;
        var p = Vector2.Lerp((Vector2)Trnsfrm.position, DesP, Cam.orthographicSize * PanSpeed * Time.deltaTime);
        Trnsfrm.position = new Vector3(p.x, p.y, PosZ);       
        /*
        float hHght = Cam.orthographicSize;
        float hWid = hHght* Cam.aspect;

        float mnX= Bounds.bounds.min.x +hWid, mxX = Bounds.bounds.max.x- hWid;
        if( mnX > mxX ) mnX = mxX = (mnX+mxX)*0.5f;
        float mnY= Bounds.bounds.min.y +hHght, mxY = Bounds.bounds.max.y- hHght;
        if( mnY > mxY ) mnY = mxY = (mnY+mxY)*0.5f;


        var p = new Vector3( Current.Trnsfrm.position.x,  Current.Trnsfrm.position.y, PosZ );
        p.x = Mathf.Clamp(p.x, mnX, mxX );
        p.y = Mathf.Clamp(p.y, mnY, mxY );

        Trnsfrm.position = p;  */       
	}
}
